#include "cjsonarray.h"
#include "cjsonerror.h"

CJsonArray::CJsonArray() :
  CJsonValue(JV_ARRAY)
{
}

CJsonArray::~CJsonArray()
{
  while (!members.empty())
  {
    if (members.back())
    {
      delete members.back();
    }
    members.pop_back();
  }
}

jstring CJsonArray::ToString()
{
    jstring result = J("[");
    for (unsigned i = 0; i < members.size(); i++)
    {
        if (i)
        {
            result += J(",");
        }
        result += members[i]->ToString();
    }
    result += J("]");
    return result;
}

void CJsonArray::AddMember(CJsonValue *member)
{
    if (!member)
    {
        throw CJsonError(INVALID_PARAM);
    }
    members.push_back(member);
}

CJsonValue * CJsonArray::operator [] (int index) const
{
  if (index >= members.size())
  {
    return NULL;
  }
  return members[index];
}

bool CJsonArray::Empty() const
{
  return members.empty();
}

std::size_t CJsonArray::Size() const
{
  return members.size();
}

